<script>
	import { public_img } from './static/public.js'
	export default {
		onLaunch: function() {
			console.log('App Launch')
			// 下载网络图标到本地
			this.downloadTabBarIcons()
		},
		onShow: function() {
			console.log('App Show')
		},
		onHide: function() {
			console.log('App Hide')
		},
		methods: {
			// 下载 tabBar 图标到本地
			downloadTabBarIcons() {
				// 定义网络图标地址
				const icons = {
					home: public_img + '/img/img17343818acb14ce48.png',
					homeActive: public_img + '/img/img17015318abca09860.png',
					category: public_img + '/img/img17014818abca09860.png',
					categoryActive: public_img + '/img/img17014818abca09860.png',
					cart: public_img + '/img/img17015718abca23670.png',
					cartActive: public_img + '/img/img17015718abca23670.png',
					user: public_img + '/img/img17015418abca09860.png',
					userActive: public_img + '/img/img17039218abf905998.png'
				}
				
				// 检查是否已经下载过图标
				const isDownloaded = uni.getStorageSync('tabbar_icons_downloaded');
				if (isDownloaded) {
					console.log('图标已存在，无需重新下载');
					return;
				}
				
				let downloadCount = 0;
				const totalIcons = Object.keys(icons).length;
				
				// 批量下载图标
				Object.keys(icons).forEach(key => {
					const url = icons[key]
					uni.downloadFile({
						url: url,
						success: (res) => {
							if (res.statusCode === 200) {
								// 保存到本地
								uni.saveFile({
									tempFilePath: res.tempFilePath,
									success: (saveRes) => {
										// 将本地路径保存到本地存储
										uni.setStorageSync(`tabbar_icon_${key}`, saveRes.savedFilePath)
										console.log(`图标 ${key} 下载并保存成功`, saveRes.savedFilePath)
										
										downloadCount++;
										// 当所有图标都下载完成时，标记为已下载
										if (downloadCount === totalIcons) {
											uni.setStorageSync('tabbar_icons_downloaded', true);
										}
									},
									fail: (err) => {
										console.error(`保存图标 ${key} 失败`, err)
									}
								})
							}
						},
						fail: (err) => {
							console.error(`下载图标 ${key} 失败`, err)
						}
					})
				})
			}
		}
	}
</script>

<style>
	/*每个页面公共css */
</style>
